/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.lifecycle

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertIs

@Suppress("deprecation")
class LifecyclingTest {
    @Test
    fun testDefaultLifecycleObserverAdapter() {
        val owner = object : LifecycleOwner {
            override val lifecycle: Lifecycle
                get() = NoOpLifecycle()
        }
        
        val testObserver = TestDefaultLifecycleObserver()
        val adapter = DefaultLifecycleObserverAdapter(testObserver, null)
        
        adapter.onStateChanged(owner, Lifecycle.Event.ON_CREATE)
        assertEquals(1, testObserver.onCreateCallCount)
        
        adapter.onStateChanged(owner, Lifecycle.Event.ON_START)
        assertEquals(1, testObserver.onStartCallCount)
        
        adapter.onStateChanged(owner, Lifecycle.Event.ON_RESUME)
        assertEquals(1, testObserver.onResumeCallCount)
        
        adapter.onStateChanged(owner, Lifecycle.Event.ON_PAUSE)
        assertEquals(1, testObserver.onPauseCallCount)
        
        adapter.onStateChanged(owner, Lifecycle.Event.ON_STOP)
        assertEquals(1, testObserver.onStopCallCount)
        
        adapter.onStateChanged(owner, Lifecycle.Event.ON_DESTROY)
        assertEquals(1, testObserver.onDestroyCallCount)
    }

    @Test
    fun testDefaultLifecycleObserverAndLifecycleEventObserver() {
        val owner = object : LifecycleOwner {
            override val lifecycle: Lifecycle
                get() = NoOpLifecycle()
        }
        
        val testObserver = TestAllObservers()
        val adapter = DefaultLifecycleObserverAdapter(testObserver, testObserver)
        
        adapter.onStateChanged(owner, Lifecycle.Event.ON_CREATE)
        assertEquals(1, testObserver.onCreateCallCount)
        assertEquals(Lifecycle.Event.ON_CREATE, testObserver.lastEvent)
        
        testObserver.reset()
        adapter.onStateChanged(owner, Lifecycle.Event.ON_START)
        assertEquals(1, testObserver.onStartCallCount)
        assertEquals(Lifecycle.Event.ON_START, testObserver.lastEvent)
    }

    @Test
    fun testOnAnyEventThrowsException() {
        val owner = object : LifecycleOwner {
            override val lifecycle: Lifecycle
                get() = NoOpLifecycle()
        }
        
        val testObserver = TestDefaultLifecycleObserver()
        val adapter = DefaultLifecycleObserverAdapter(testObserver, null)
        
        assertFailsWith<IllegalArgumentException> {
            adapter.onStateChanged(owner, Lifecycle.Event.ON_ANY)
        }
    }

    private class TestDefaultLifecycleObserver : DefaultLifecycleObserver {
        var onCreateCallCount = 0
        var onStartCallCount = 0
        var onResumeCallCount = 0
        var onPauseCallCount = 0
        var onStopCallCount = 0
        var onDestroyCallCount = 0
        
        override fun onCreate(owner: LifecycleOwner) {
            onCreateCallCount++
        }
        
        override fun onStart(owner: LifecycleOwner) {
            onStartCallCount++
        }
        
        override fun onResume(owner: LifecycleOwner) {
            onResumeCallCount++
        }
        
        override fun onPause(owner: LifecycleOwner) {
            onPauseCallCount++
        }
        
        override fun onStop(owner: LifecycleOwner) {
            onStopCallCount++
        }
        
        override fun onDestroy(owner: LifecycleOwner) {
            onDestroyCallCount++
        }
    }

    private class TestAllObservers : DefaultLifecycleObserver, LifecycleEventObserver {
        var onCreateCallCount = 0
        var onStartCallCount = 0
        var lastEvent: Lifecycle.Event? = null
        
        fun reset() {
            onCreateCallCount = 0
            onStartCallCount = 0
            lastEvent = null
        }
        
        override fun onCreate(owner: LifecycleOwner) {
            onCreateCallCount++
        }
        
        override fun onStart(owner: LifecycleOwner) {
            onStartCallCount++
        }
        
        override fun onResume(owner: LifecycleOwner) {}
        override fun onPause(owner: LifecycleOwner) {}
        override fun onStop(owner: LifecycleOwner) {}
        override fun onDestroy(owner: LifecycleOwner) {}
        
        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            lastEvent = event
        }
    }

    // 简化的NoOpLifecycle实现
    private class NoOpLifecycle : Lifecycle() {
        override val currentState: State
            get() = State.INITIALIZED
        
        override fun addObserver(observer: LifecycleObserver) {
            // 简化实现，仅用于测试
        }
        
        override fun removeObserver(observer: LifecycleObserver) {
            // 简化实现，仅用于测试
        }
    }
}